/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

/**
 *
 * @author DOStudent1
 */
public class DateForDb {

    final static DateFormat formatForDb=DateFormat.getDateInstance(DateFormat.DEFAULT);

    final static DateFormat formatForDisplay=DateFormat.getDateInstance(DateFormat.LONG);

    Date date;
    
    public static DateForDb fromDate(Date date) {
        return(new DateForDb(date));
    }

    public Date toDate() {
        return(date);
    }
    
    public static DateForDb fromDbString(String str) throws ParseException {
        return(new DateForDb(formatForDb.parse(str)));
    }
    
    private DateForDb(Date date) {
        this.date=date;
    }
    
    public String toDbString() {
        return(formatForDb.format(date));
    }

 
    public static DateForDb fromDisplayString(String str) throws ParseException {
        return(new DateForDb(formatForDisplay.parse(str)));
    }

    public String toDisplayString() {
        return(formatForDisplay.format(date));
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DateForDb other = (DateForDb) obj;
        return date.equals(other.date);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + (this.date != null ? this.date.hashCode() : 0);
        return hash;
    }
    
}
